define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'admin_project/index' + location.search,
                    add_url: 'admin_project/add',
                    edit_url: 'admin_project/edit',
                    del_url: 'admin_project/del',
                    multi_url: 'admin_project/multi',
                    table: 'admin_project',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'uid', title: __('Uid')},
                        {field: 'pno', title: __('Pno')},
                        {field: 'pid', title: __('Pid')},
                        {field: 'group_id', title: __('Group_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        pmanager: function() {
            Controller.api.bindevent();

            Table.api.init({
                extend: {
                    index_url: 'admin_project/index' + location.search,
                    del_url: 'admin_project/del',
                    table: 'admin_project',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: true,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: false,
                showSearch: false,
                queryParams: function(params) {
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.project_id = Config.project_id;
                    params.op.project_id = '=';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('姓名')},
                        {field: 'group_name', title: __('组')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".btn-add").click(function(event) {
                var uid = $("#uid").val();
                if(uid){
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: location.href ,//url
                        data: {uid: uid},
                        success: function (ret) {
                            table.bootstrapTable('refresh');
                            parent.$(".btn-refresh").trigger("click");
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                }
                else{
                    Layer.alert(__('请选择人员 '));
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});